package ui.units {
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	import globals.Global;

	public class Bullet extends BaseUnit {
		
		public var speed:uint;
		public var start:Point;
		public var end:Point;
		public var target:Monster;
//		public var ac

		public function Bullet() {
			super();
		}
		
		override public function init(bean:UnitBean):void {
			super.init(bean);
			end = new Point();
			view.x = - view.width;
			if (bean.url == "Bullet3") {
				scaleX = scaleY = 2;
			} else if (bean.url == "Bullet2") {
				scaleX = scaleY = 0.6;
			}
		}
		
		override public function update():void {
			if (target == null) {
				return;
			}
			end.x = target.x;
			end.y = target.y;
			super.update();
			var angle:Number = Math.atan2(end.y - start.y, end.x - start.x);
			rotation = angle * 180 / Math.PI;
			var dx:Number = speed * Math.cos(angle);
			var dy:Number = speed * Math.sin(angle);
			if (((end.x - x) * (end.x - x) + (end.y - y) * (end.y - y)) <= bean.attackRange * bean.attackRange) {
				hit();
			} else {
				x += dx;
				y += dy;
			}
		}
		
		public function hit():void {
			x = end.x;
			y = end.y;
			
			if (target != null) {
				target.hitBlood(bean.damage);
			}
			
			Global.unitManager.removeBullet(id);
		}
	}
}